package me.xiaosiqi.web.controller.store;

import com.github.pagehelper.PageInfo;
import me.xiaosiqi.domain.store.Catalog;
import me.xiaosiqi.domain.store.Course;
import me.xiaosiqi.utils.BeanUtil;
import me.xiaosiqi.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author xiaosiqi
 * @date 2021/6/3 18:01
 */

@WebServlet("/store/catalog")
public class CatalogServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/javascript;charset=utf-8");
        String operation = req.getParameter("operation");
        System.out.println(operation);
        if ("list".equals(operation)){
            // uri:/store/catalog?operation=list
            list(req,resp);
        }else if ("delete".equals(operation)){
            // uri:/store/catalog?operation=delete
            delete(req,resp);
        }else if ("toAdd".equals(operation)){
            toAdd(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            edit(req,resp);
        }



    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.update(catalog);
        resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Catalog catalog = catalogService.findById(id);
        req.setAttribute("catalog",catalog);
        List<Course> courseList = courseService.findAll();
        req.setAttribute("courseList",courseList);


        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/catalog/update.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Catalog catalog  = BeanUtil.fillBean(req,Catalog.class,"yyyy-MM-dd");
        catalogService.save(catalog);
        resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跳转页面
        List<Course> courseList = courseService.findAll();
        req.setAttribute("courseList",courseList);
        req.getRequestDispatcher("/WEB-INF/pages/store/catalog/add.jsp").forward(req,resp);

    }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Catalog catalog = new Catalog();
        catalog.setId(id);
        catalogService.delete(catalog);
        resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 15;
       if (StringUtils.isNoneBlank(req.getParameter("page"))){
           page = Integer.parseInt(req.getParameter("page"));
       }
        if (StringUtils.isNoneBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo pageInfo = catalogService.findPage(page, size);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/store/catalog/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
